/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sais.noisylibrary.core;

import java.util.ArrayList;
import java.util.HashSet;
import org.sais.noisylibrary.card.AttributeCard;
import org.sais.noisylibrary.card.HelperCard;
import org.sais.noisylibrary.card.SpellCard;

/**
 *
 * @author Romulus
 */
public class Player {

    private String mName;
    private HashSet<AttributeCard> mAttrHand = new HashSet<AttributeCard>();
    private HashSet<SpellCard> mSpellHand = new HashSet<SpellCard>();
    private HashSet<HelperCard> mHelperHand = new HashSet<HelperCard>();

    public Player(String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public void gainAttrCard(AttributeCard card) {
        mAttrHand.add(card);
    }

    public HashSet<AttributeCard> gainSpellCard(SpellCard card) {
        HashSet<AttributeCard> removal = new HashSet<AttributeCard>();
        int[] a = toConsumeArray();
        for (int i = 0; i < 5; ++i) {
            if (a[i] < card.getCostArray()[i]) {
                return null;
            }
        }
        mSpellHand.add(card);
        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < card.getCostArray()[i]; ++j) {
                AttributeCard ac = null;
                for (AttributeCard c : mAttrHand) {
                    if (c.getAttribute() == i) {
                        ac = c;
                        break;
                    }
                }
                mAttrHand.remove(ac);
                removal.add(ac);
            }
        }
        return removal;
    }

    public void gainSpellCardForced(SpellCard card) {
        mSpellHand.add(card);
    }

    private int[] toConsumeArray() {
        int[] ret = new int[5];
        for (AttributeCard c : mAttrHand) {
            ret[c.getAttribute()]++;
        }
        return ret;
    }

    public String getAttrHandString() {
        String ret = "";
        for (AttributeCard c : mAttrHand) {
            ret += c.toString() + " ";
        }
        return ret;
    }

    public int currentPoints() {
        int ret = 0;
        for (SpellCard c : mSpellHand) {
            ret += c.getPoint();
        }
        return ret;
    }

    public AttributeCard discard(String target) {
        for (AttributeCard c : mAttrHand) {
            if (c.toChar() == target.charAt(0) || c.getName().contains(target)) {
                mAttrHand.remove(c);
                return c;
            }
        }
        return null;
    }

    public SpellCard dispell(String target) {
        for (SpellCard c : mSpellHand) {
            if (c.getName().contains(target)) {
                mSpellHand.remove(c);
                return c;
            }
        }
        return null;
    }

    public boolean hasHelper() {
        return mHelperHand.size() > 0;
    }

    public HashSet<SpellCard> getSpellHand() {
        return mSpellHand;
    }

    public void gainHelperCard(HelperCard card) {
        mHelperHand.add(card);
    }

    public HelperCard consumeHelper() {
        if (mHelperHand.size() == 0) {
            return null;
        }
        HelperCard c = mHelperHand.iterator().next();
        mHelperHand.remove(c);
        return c;
    }

    public HelperCard consumeHelper(String target) {
        for (HelperCard c : mHelperHand) {
            if (c.getName().contains(target)) {
                mHelperHand.remove(c);
                return c;
            }
        }
        return null;
    }

    /*public String getSpellSummary() {
        StringBuffer b = new StringBuffer();
        for (SpellCard c : mSpellHand) {
            b.append(c.getCostString() + "(" + c.getPoint() + ") ");
        }
        return b.toString();
    }*/

}
